# coding:utf-8

import os
import random

from base import Base
from common import error
from common import utils


class User(Base):
    def __init__(self, username, user_json, gift_json):
        self.username = username
        self.gift_random = list(range(1, 101))
        super().__init__(user_json, gift_json)
        self.get_user()

    def get_user(self):
        users = self._Base__read_users()
        if self.username not in users:
            raise error.NotUserError('not user %s ' % self.username)

        current_user = users.get(self.username)

        if current_user.get('active') == False:
            raise error.UserActiveError('the user %s had not use' % self.username)
        if current_user.get('role') != 'normal':
            raise error.RoleError('permission')

        self.user = current_user
        self.name = current_user.get('username')
        self.role = current_user.get('role')
        self.gifts = current_user.get('gifts')
        self.create_time = utils.timestamp_to_string(current_user.get('create_time'))

    def get_gifts(self):
        gifts = self._Base__read_gifts()
        gift_lists = []

        for level_one, level_one_pool in gifts.items():
            for level_two, level_two_pool in level_one_pool.items():
                for gift_name, gift_info in level_two_pool.items():
                    gift_lists.append(gift_info.get('name'))
        return gift_lists

    def choice_gift(self):
        self.get_user()
        first_level, second_level = None, None
        level_one_count = random.choice(self.gift_random)
        if 1 <= level_one_count <= 50:
            first_level = 'level1'
        elif 51 <= level_one_count <= 80:
            first_level = 'level2'
        elif 81 <= level_one_count < 95:
            first_level = 'level3'
        elif 95 <= level_one_count <= 100:
            first_level = 'level4'
        else:
            raise error.CountError('level_one_count not in 1-100')

        gifts = self._Base__read_gifts()
        level_one = gifts.get(first_level)
        level_two_count = random.choice(self.gift_random)

        if 1 <= level_two_count <= 80:
            second_level = 'level1'
        elif 81 <= level_two_count < 95:
            second_level = 'level2'
        elif 95 <= level_two_count <= 100:
            second_level = 'level3'
        else:
            raise error.CountError('level_two_count not in 1-100')

        level_two = level_one.get(second_level)
        if len(level_two) == 0:
            print('哦，可惜您没中奖')
            return

        gift_name = random.choice(list(level_two.keys()))
        gift_info = level_two.get(gift_name)
        if gift_info.get('count') == 0:
            print('哦，可惜您没中奖')
            return
        gifts[first_level][second_level][gift_name]['count'] -= 1
        self._Base__save(gifts, self.gift_json)
        self.user['gifts'].append(gift_name)
        self.update()
        print('恭喜您获得 %s 奖品' % gift_name)

    def update(self):
        users = self._Base__read_users()
        users[self.username] = self.user
        self._Base__save(users, user_json)


if __name__ == '__main__':
    user_json = os.path.join(os.getcwd(), 'storage', 'user.json')
    gift_json = os.path.join(os.getcwd(), 'storage', 'gift.json')

    user = User('mary', user_json, gift_json)
    # print(user.name, user.create_time, user.gifts, user.role)
    # user.get_gifts()
    user.choice_gift()
